<template>
	<jk-dialog
		v-model="showDialog"
		:title="`${pageType === 'edit' ? '编辑' : pageType === 'add' ? '新增' : '详情'}-验收入库单`"
		width="1300px"
		:fullscreen="true"
		:show-content-loading="showLoading"
		:confirm-btn-loading="saveBtnLoading"
		:show-confirm-btn="pageType !== 'detail'"
		:show-cancel-btn="pageType !== 'detail'"
		:show-close-btn="pageType === 'detail'"
		@on-close="onVisibleChange(false)"
		@on-cancel="onVisibleChange(false)"
		@on-visible-change="onVisibleChange"
		@on-confirm="onConfirmBtn"
	>
		<el-form ref="ruleForm" :model="ruleForm" :rules="rules" :hide-required-asterisk="pageType === 'detail' || ruleForm.auditState === 1" label-width="100px" :show-message="false">
			<div ref="box1">
				<div class="flex-between-center margin-bottom-4">
					<el-tag type="primary" effect="plain">
						<span class="el-icon-tickets item-gap"></span>
						<span>基本信息</span>
					</el-tag>
					<div class="flex-end-center">
					</div>
				</div>
				<jk-card class="margin-bottom-10">
					<el-row>
						<el-col :span="6">
							<el-form-item class="margin-bottom-10" label="单据编号">
								<span class="read-only-2">{{ ruleForm.code || '系统自动生成' }}</span>
							</el-form-item>
						</el-col>
						<el-col :span="6">
							<el-form-item class="margin-bottom-10" label="日期" prop="belongDate">
								<span v-if="pageType === 'detail' || ruleForm.auditState === 1" class="read-only-2">{{ ruleForm.belongDate }}</span>
								<jk-date-picker v-else v-model="ruleForm.belongDate" size="medium" placeholder="日期" />
							</el-form-item>
						</el-col>
						<el-col :span="6">
							<el-form-item class="margin-bottom-10" label="仓库" prop="minValue">
								<span v-if="pageType === 'detail' || ruleForm.auditState === 1" class="read-only-2">{{ ruleForm.minValue }}</span>
								<jk-select v-else v-model="ruleForm.minValue" class="width-100-percent" size="medium" placeholder="仓库" />
							</el-form-item>
						</el-col>
						<el-col :span="6">
							<el-form-item class="margin-bottom-10" label="到货单" prop="minValue">
								<span v-if="pageType === 'detail' || ruleForm.auditState === 1" class="read-only-2">{{ ruleForm.minValue }}</span>
								<jk-select v-else v-model="ruleForm.minValue" class="width-100-percent" size="medium" placeholder="到货单" />
							</el-form-item>
						</el-col>
						<el-col :span="6">
							<el-form-item class="margin-bottom-10" label="车号">
								<span class="read-only-2">{{ ruleForm.code2 }}</span>
							</el-form-item>
						</el-col>
						<el-col :span="6">
							<el-form-item class="margin-bottom-10" label="供应商">
								<span class="read-only-2">{{ ruleForm.code2 }}</span>
							</el-form-item>
						</el-col>
						<el-col :span="6">
							<el-form-item class="margin-bottom-10" label="部门">
								<span v-if="pageType === 'detail' || ruleForm.auditState === 1" class="read-only-2">{{ ruleForm.minValue }}</span>
								<jk-select v-else v-model="ruleForm.minValue" class="width-100-percent" size="medium" placeholder="" />
							</el-form-item>
						</el-col>
						<el-col :span="6">
							<el-form-item class="margin-bottom-10" label="业务员">
								<span class="read-only-2">{{ ruleForm.code2 }}</span>
							</el-form-item>
						</el-col>
						<el-col :span="6">
							<el-form-item class="margin-bottom-10" label="入库类别">
								<span v-if="pageType === 'detail' || ruleForm.auditState === 1" class="read-only-2">{{ ruleForm.minValue }}</span>
								<jk-select v-else v-model="ruleForm.minValue" class="width-100-percent" size="medium" placeholder="" />
							</el-form-item>
						</el-col>
						<el-col :span="6">
							<el-form-item class="margin-bottom-10" label="业务类型">
								<span class="read-only-2">{{ ruleForm.code2 }}</span>
							</el-form-item>
						</el-col>
						<el-col :span="6">
							<el-form-item class="margin-bottom-10" label="采购类型">
								<span class="read-only-2">{{ ruleForm.code2 }}</span>
							</el-form-item>
						</el-col>
						<el-col :span="6">
							<el-form-item class="margin-bottom-10" label="备注">
								<span class="read-only-2">{{ ruleForm.code2 }}</span>
							</el-form-item>
						</el-col>
						<el-col :span="6">
							<el-form-item class="margin-bottom-10" label="发票重量T">
								<span v-if="pageType === 'detail' || ruleForm.auditState === 1" class="read-only-2">{{ ruleForm.minValue }}</span>
								<jk-number-input v-else v-model="ruleForm.minValue" class="width-100-percent" size="medium" placeholder="" />
							</el-form-item>
						</el-col>
						<el-col :span="6">
							<el-form-item class="margin-bottom-10" label="税率">
								<span v-if="pageType === 'detail' || ruleForm.auditState === 1" class="read-only-2">{{ ruleForm.minValue }}</span>
								<jk-number-input v-else v-model="ruleForm.minValue" class="width-100-percent" size="medium" placeholder="" />
							</el-form-item>
						</el-col>
						<el-col :span="6">
							<el-form-item class="margin-bottom-10" label="入库人" prop="minValue">
								<span v-if="pageType === 'detail' || ruleForm.auditState === 1" class="read-only-2">{{ ruleForm.minValue }}</span>
								<jk-select v-else v-model="ruleForm.minValue" class="width-100-percent" size="medium" placeholder="入库人" />
							</el-form-item>
						</el-col>
						<el-col :span="24">
							<el-form-item class="margin-bottom-0" label="备注">
								<span v-if="pageType === 'detail' || ruleForm.auditState === 1" class="read-only-2">{{ ruleForm.remark }}</span>
								<el-input v-else v-model="ruleForm.remark" :rows="1" type="textarea" placeholder="" />
							</el-form-item>
						</el-col>
					</el-row>
				</jk-card>
			</div>
			<div ref="box2" class="flex-between-center margin-bottom-4">
				<el-tag type="primary" effect="plain">
					<span class="el-icon-document item-gap"></span>
					<span>明细</span>
				</el-tag>
				<div class="flex-end-center"></div>
			</div>
			<jk-table border :data="ruleForm.configItem" :max-height="dialogTableHeight - 50">
				<vxe-column title="存货编号" align="left" field="unitPrice" :min-width="110"></vxe-column>
				<vxe-column title="存货名称" align="left" field="unitPrice" :min-width="110"></vxe-column>
				<vxe-column title="批号" align="left" field="unitPrice" :min-width="110"></vxe-column>
				<vxe-column title="单位" align="left" field="unitPrice" :min-width="110"></vxe-column>
				<vxe-column title="件数" align="left" field="unitPrice" :min-width="110"></vxe-column>
				<vxe-column title="换算率" align="left" field="unitPrice" :min-width="110"></vxe-column>
				<vxe-column title="数量(公重)" align="left" field="unitPrice" :min-width="110"></vxe-column>
				<vxe-column title="品级" align="left" field="unitPrice" :min-width="110"></vxe-column>
				<vxe-column title="长度" align="left" field="unitPrice" :min-width="110"></vxe-column>
				<vxe-column title="皮重(10g)" align="left" field="unitPrice" :min-width="110"></vxe-column>
				<vxe-column title="净重(T)" align="left" field="unitPrice" :min-width="110"></vxe-column>
				<vxe-column title="回潮" align="left" field="unitPrice" :min-width="110"></vxe-column>
				<vxe-column title="含杂" align="left" field="unitPrice" :min-width="110"></vxe-column>
				<vxe-column title="公重" align="left" field="unitPrice" :min-width="110"></vxe-column>
				<vxe-column title="仓位" align="left" field="unitPrice" :min-width="110"></vxe-column>
				<vxe-column title="类型" align="left" field="unitPrice" :min-width="110"></vxe-column>
				<vxe-column title="单价" align="left" field="unitPrice" :min-width="110"></vxe-column>
			</jk-table>
		</el-form>
	</jk-dialog>
</template>

<script>
    import calHeight from '../../../../mixins/cal-dialog-height';
    import { spinYarnCountPriceConfigCreate, spinYarnCountPriceConfigUpdate, spinYarnCountPriceConfigGet } from '../../../../api/material/yarnCounPrice';
    import remoteSearchProductInput from '../../../technology/order/components/remote-search-product-input';
    export default {
        mixins: [calHeight],
        components: { remoteSearchProductInput },
        props: {
            pageType: {
                type: String,
                default: ''
            },
            dialogState: {
                type: Boolean,
                default: false
            },
            dataId: {
                type: [String, Number],
                default: ''
            }
        },
        data() {
            return {
                processList: [],
                tableLoading: false,
                saveBtnLoading: false,
                workshopList: [],
                showDialog: false,
                showLoading: true,
                confirmBtnLoading: false,
                saveDialogState: false,
                ruleForm: {
                    'minValue': null,
                    'maxValue': null,
                    'auditState': 0,
                    'remark': '',
                    'configItem': [
                        {
                            batchList: [
                                { batchCode: '111111' },
                                { batchCode: '111111' },
                                { batchCode: '111111' }
                            ]
                        }
                    ]
                },
                rules: {
                    minValue: [{ required: true, trigger: 'change' }],
                    maxValue: [{ required: true, trigger: 'change' }],
                    criterion: [{ required: true, trigger: 'change' }],
                    tenPrice: [{ required: true, trigger: 'change' }]
                },
                defaultRowData: {
                    'processId': '',
                    'processCode': '',
                    'processName': '',
                    'unitPrice': 0,
                    batchList: []
                }
            };
        },
        methods: {
            onChildAddBtn(rowIndex, batchIndex) {
                if (batchIndex || batchIndex === 0) {
                    this.ruleForm.configItem[rowIndex].batchList.splice(batchIndex + 1, 0, {});
                } else {
                    this.ruleForm.configItem[rowIndex].batchList.push({});
                }
            },
            onDeleteBtn(scope) {
                this.ruleForm.configItem.splice(scope.rowIndex, 1);
            },
            onAddBtn() {
                this.ruleForm.configItem.push(JSON.parse(JSON.stringify(this.defaultRowData)));
            },
            getParamData() {
                const params = JSON.parse(JSON.stringify(this.ruleForm));
                if (!this.dataId) {
                    params.auditState = 1;
                    delete params.id;
                }
                let colorItem = {};
                params.configItem.forEach(x => {
                    colorItem = this.processList.find(k => k.id === x.processId) || {};
                    x.processCode = colorItem.code;
                    x.processName = colorItem.name;
                });
                return params;
            },
            onConfirmBtn() {
                this.$refs['ruleForm'].validate(valid => {
                    if (valid) {
                        this.createAndUpdateRequest();
                    } else {
                        this.$tipsBar(this, 'validator');
                    }
                });
            },
            createAndUpdateRequest() {
                const api = !this.dataId ? spinYarnCountPriceConfigCreate : spinYarnCountPriceConfigUpdate;
                api(this.getParamData()).then(res => {
                    this.saveBtnLoading = false;
                    if (res.data.status === 200) {
                        this.$tipsBar(this, 'save');
                        this.$emit('on-finish', res.data.res);
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                    this.$nextTick(() => {
                        this.$refs.ruleForm.resetFields();
                    });
                }
            },
            getDetailRequest() {
                return spinYarnCountPriceConfigGet({ id: this.dataId }).then(res => {
                    if (res.data.status === 200) {
                        const resData = res.data.res || {};
                        resData.configItem = resData.configItem || [];
                        this.ruleForm = res.data.res;
                    }
                });
            },
            async getDependentData() {
                this.showLoading = true;
                this.processList = this.$store.state.user.userProcess.processList || [];
                if (this.dataId) await this.getDetailRequest();
                this.showLoading = false;
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.$nextTick(() => {
                        this.$refs.ruleForm.clearValidate();
                    });
                    this.getDependentData();
                }
            }
        }
    };
</script>
